<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/jsp/commons/global.jsp"%>

<table id="videoNotEntityList" style="height: 100%; width: 100%;">

</table>

<div id="videoNotEntityListToolBars" style="height: auto">
	<a href="javascript:void(0)" class="easyui-linkbutton"
		data-options="iconCls:'icon-add',plain:true"
		onclick="videoNotEntityList_append()">添加</a>|<a
		href="javascript:void(0)" class="easyui-linkbutton"
		data-options="iconCls:'icon-edit',plain:true"
		onclick="videoNotEntityList_edit()">编辑</a> |<a
		href="javascript:void(0)" class="easyui-linkbutton"
		data-options="iconCls:'icon-undo',plain:true"
		onclick="videoNotEntityList_undo()">取消</a>| <a
		href="javascript:void(0)" class="easyui-linkbutton"
		data-options="iconCls:'icon-save',plain:true"
		onclick="videoNotEntityList_accept()">保存</a>| <a
		href="javascript:void(0)" class="easyui-linkbutton"
		data-options="iconCls:'fa fa-cog',plain:true"
		onclick="videoNotEntityPageRecommendVideoList_add()">推荐</a>|<a
		href="javascript:void(0)" class="easyui-linkbutton"
		data-options="iconCls:'fa fa-cog',plain:true"
		onclick="videoNotEntityList_categoryListPage()">设置分类</a>|<a
		href="javascript:void(0)" class="easyui-linkbutton"
		data-options="iconCls:'fa fa-cog',plain:true"
		onclick="videoNotEntityList_tagListPage()">设置标签</a> |<a
		href="javascript:void(0)" class="easyui-linkbutton"
		data-options="iconCls:'fa fa-cog',plain:true"
		onclick="videoNotEntityChildList_config()">子视频管理</a> |<input
		id="videoNotEntityChildList_StatusQue" style="width: 100px;" /> |<input
		id="videoNotEntityChildList_ProgressStatusQue" style="width: 100px;" />
	|<input id="videoNotEntityTitleQue" style="width: 200px;" />
</div>

<script type="text/javascript">
	function videoNotEntityPageRecommendVideoList_add() {
		var videoSelect = videoNotEntityListDatagrid.treegrid('getSelected');
		if (videoSelect) {
			$.messager.confirm('确认对话框', '确定推荐吗?', function(r) {
				if (r) {
					var _Data = {
						itemType : 1,
						itemId : videoSelect.id
					};

					$.ajax({
						data : _Data,
						async : false,
						url : baseUrl + '/recommend/add',
						success : function(data) {
							if (data.responseCode == '000') {
								$.messager.alert('提示', '推荐成功', 'info');
							} else {
								$.messager.alert('提示', data.responseMessage,
										'error');
							}
						}
					});
				}
			});
		}
	}

	function videoNotEntityListDatagridReload() {
		try {
			var queryParams = {
				isEntity : false,
				like_title : videoNotEntityTitleQue.textbox('getValue'),
				status : videoNotEntityChildList_StatusQue.combobox('getValue'),
				progressStatus : videoNotEntityChildList_ProgressStatusQue
						.combobox('getValue')
			};
			videoNotEntityListDatagrid.treegrid("options").queryParams = queryParams;
			videoNotEntityListDatagrid.treegrid("reload");
		} catch (e) {
		}
	}
</script>

<script type="text/javascript">
	var videoNotEntityChildList_ProgressStatusQue = undefined;
	var videoNotEntityChildList_StatusQue = undefined;
	var videoNotEntityTitleQue = undefined;
	var notEntityVideoEditRow = undefined;
	var videoNotEntityListDatagrid = undefined;

	$(function() {
		videoNotEntityChildList_ProgressStatusQue = $(
				'#videoNotEntityChildList_ProgressStatusQue').combobox({
			data : [ {
				"progressStatusCode" : 0,
				"progressStatusMsg" : "全部进度",
				"selected" : true
			}, {
				"progressStatusCode" : 1,
				"progressStatusMsg" : "连载中"
			}, {
				"progressStatusCode" : 2,
				"progressStatusMsg" : "已完结"
			} ],
			valueField : 'progressStatusCode',
			textField : 'progressStatusMsg',
			onChange : function(newValue, oldValue) {
				videoNotEntityListDatagridReload();
			}
		});

		videoNotEntityChildList_StatusQue = $(
				'#videoNotEntityChildList_StatusQue').combobox({
			data : [ {
				"statusCode" : 0,
				"statusMsg" : "全部状态",
				"selected" : true
			}, {
				"statusCode" : 1,
				"statusMsg" : "已发布"
			}, {
				"statusCode" : 2,
				"statusMsg" : "已下线"
			}, {
				"statusCode" : 3,
				"statusMsg" : "待发布"
			} ],
			valueField : 'statusCode',
			textField : 'statusMsg',
			onChange : function(newValue, oldValue) {
				videoNotEntityListDatagridReload();
			}
		});

		videoNotEntityTitleQue = $('#videoNotEntityTitleQue').textbox({
			buttonText : '标题搜索',
			buttonIcon : 'icon-search',
			buttonAlign : 'right'
		});

		videoNotEntityTitleQue.textbox('button').click(function() {
			videoNotEntityListDatagridReload();
		});

		videoNotEntityListDatagrid = $('#videoNotEntityList')
				.treegrid(
						{
							url : '${baseUrl}/video/list',
							pagination : true,
							sortName : 'sort,updateTime',
							sortOrder : 'desc,desc',
							multiSort : true,
							//remoteSort : false,
							// 			rownumbers : true,
							queryParams : {
								isEntity : false
							},
							singleSelect : true,
							idField : 'id',
							treeField : 'title',
							striped : true,
							pageSize : 20,
							fitColumns : true,
							lines : true,
							toolbar : '#videoNotEntityListToolBars',
							onLoadSuccess : function() {
								notEntityVideoEditRow = undefined;
							},
							onBeforeSelect : function(row) {
								if (notEntityVideoEditRow) {
									return false;
								}
							},
							onBeforeEdit : function(row) {
								if (notEntityVideoEditRow == undefined) {
									notEntityVideoEditRow = row;
								}
							},
							onAfterEdit : function(row, changes) {
								notEntityVideoEditRow = undefined;
							},
							onCancelEdit : function(row) {
								notEntityVideoEditRow = undefined;
							},
							onClickCell : function(field, row) {

								if (notEntityVideoEditRow == undefined
										&& videoNotEntityListDatagrid.treegrid(
												'getLevel', row.id) > 1
										&& field == 'sort') {
									videoNotEntityListDatagrid.treegrid(
											'selectRow', row.id).treegrid(
											'editCell', {
												id : row.id,
												field : field
											});
									notEntityVideoEditRow = row;
								}
							},
							// 			nowrap : false,
							columns : [ [
									{
										field : 'cover_url',
										title : '封面',
										width : 100,
										align : 'center',
										formatter : function(value, row, index) {
											if (row.coverUrl) {
												var img = '<img src="'
														+ row.coverUrl
														+ '" style="width:103%;height:100px;"/>';
												return img;
											} else {
												return value;
											}
										}
									},
									{
										field : 'id',
										title : 'ID',
										align : 'center',
										width : 60
									},
									{
										field : 'title',
										title : '标题',
										width : 249,
										editor : {
											type : 'text',
											options : {
												required : true
											}
										}
									},
									{
										field : 'createTime',
										title : '创建时间',
										width : 100
									},
									{
										field : 'updateTime',
										title : '更新时间',
										width : 100,
										sortable : true
									},
									{
										field : 'createUser',
										title : '创建者',
										width : 60,
										formatter : function(value, row, index) {
											if (row.createUser) {
												return row.createUser.nickname;
											} else {
												return value;
											}
										}
									},
									{
										field : 'entity',
										title : '视频实体',
										width : 80,
										align : 'center',
										formatter : function(value, row, index) {
											return value;
										}
									},
									{
										field : 'sort',
										title : '排序',
										width : 60,
										align : 'center',
										sortable : true,
										editor : {
											type : 'numberbox',
											options : {
												min : 0,
												precision : 2,
												required : true
											}
										}
									},
									{
										field : 'summary',
										title : '简介',
										width : 100,
										align : 'left',
										editor : {
											type : 'textarea'
										}
									},
									{
										field : 'info',
										title : '详细介绍',
										width : 100,
										align : 'left',
										editor : {
											type : 'textarea'
										}
									},
									{
										field : 'countCognate',
										title : '同源',
										width : 100,
										formatter : function(value, row, index) {
											return value + "个";
										}
									},
									{
										field : 'countCognatePlaytime',
										title : '播放总时间(秒)',
										width : 100,
										align : 'left',
										formatter : function(value, row, index) {
											if (row.entity && row.entity == '是') {
												return row.playtime;
											}
											return value;
										}
									},
									{
										field : 'statusCode',
										title : '状态',
										width : 80,
										align : 'center',
										formatter : function(value, row, index) {
											var statusCodeSpan = '<span style="';
											if (row.statusCode == 1) {
												statusCodeSpan += 'color:#6B8E23;font-size:13px;font-weight:600;';
											} else if (row.statusCode == 2) {
												statusCodeSpan += 'color:#8B4726;font-size:13px;font-weight:600;';
											} else if (row.statusCode == 3) {
												statusCodeSpan += 'color:#104E8B;font-size:13px;font-weight:600;';
											}
											return statusCodeSpan += '">'
													+ row.statusMsg + '</span>';
										},
										editor : {
											type : 'combobox',
											options : {
												valueField : 'statusCode',
												textField : 'statusMsg',
												data : [ {
													statusCode : 1,
													statusMsg : '已发布'
												}, {
													statusCode : 2,
													statusMsg : '已下线'
												}, {
													statusCode : 3,
													statusMsg : '待发布'
												} ],
												required : true
											}
										}
									},
									{
										field : 'progressStatusCode',
										title : '进度',
										width : 80,
										align : 'center',
										formatter : function(value, row, index) {
											var statusCodeSpan = '<span style="';
											if (row.progressStatusCode == 2) {
												statusCodeSpan += 'color:#6B8E23;font-size:13px;font-weight:600;';
											} else if (row.progressStatusCode == 1) {
												statusCodeSpan += 'color:#FFEC8B;font-size:13px;font-weight:600;';
											}
											return statusCodeSpan += '">'
													+ row.progressStatusMsg
													+ '</span>';
										},
										editor : {
											type : 'combobox',
											options : {
												valueField : 'statusCode',
												textField : 'statusMsg',
												data : [ {
													statusCode : 1,
													statusMsg : '连载中'
												}, {
													statusCode : 2,
													statusMsg : '已完结'
												} ],
												required : true
											}
										}
									} ] ],
							onDblClickRow : function(row) {
								$.ajax({
									data : {
										id : row.id
									},
									async : false,
									url : baseUrl + '/video/info.html',
									success : function(html) {
										$('body').append(html);
									}
								});

							}
						});

		videoNotEntityListDatagrid.treegrid('getPager').pagination(
				{
					buttons : [ {
						iconCls : 'fa fa-2x fa-location-arrow',
						handler : function() {
							videoNotEntityListDatagrid.treegrid('gotoPage',
									videoNotEntityListDatagrid.treegrid(
											'getPager').find('.pagination-num')
											.val());
						}
					} ]
				});

	});

	function videoNotEntityList_append() {

		$.ajax({
			url : baseUrl + '/video/add/page.html?isEntity=false',
			success : function(html) {
				$('body').append(html);
			}
		});
	}

	function videoNotEntityList_edit() {

		var videoSelect = videoNotEntityListDatagrid.treegrid('getSelected');
		if (notEntityVideoEditRow != undefined
				&& notEntityVideoEditRow.id != videoSelect.id) {
			$.messager.alert('提示', '请完成已开启编辑行', 'warning');
			return;
		}

		// 		if (videoNotEntityListDatagrid.treegrid('getLevel', videoSelect.id) != 1) {
		// 			return;
		// 		}

		if (videoSelect) {
			videoNotEntityListDatagrid.treegrid('beginEdit', videoSelect.id);
		}
	}

	function videoNotEntityList_undo() {
		if (notEntityVideoEditRow != undefined) {
			videoNotEntityListDatagrid.treegrid('cancelEdit',
					notEntityVideoEditRow.id);
		}
	}

	function videoNotEntityList_accept() {
		if (notEntityVideoEditRow != undefined) {

			if (!videoNotEntityListDatagrid.treegrid('validateRow',
					notEntityVideoEditRow.id)) {
				return;
			}

			//验证发布状态
			var _edStatusCode = videoNotEntityListDatagrid.treegrid(
					'getEditor', {
						id : notEntityVideoEditRow.id,
						field : 'statusCode'
					});

			var _StatusCode = 0;

			if (_edStatusCode) {
				var _statusMsg = $(_edStatusCode.target).combobox('getText');
				if (_statusMsg != '已发布' && _statusMsg != '待发布'
						&& _statusMsg != '已下线') {
					$.messager.alert('提示', '未知状态', 'warning');
					return;
				}
				_StatusCode = $(_edStatusCode.target).combobox('getValue');
			} else {
				_StatusCode = notEntityVideoEditRow.statusCode;
			}

			//验证发布状态
			var _edprogressStatusCode = videoNotEntityListDatagrid.treegrid(
					'getEditor', {
						id : notEntityVideoEditRow.id,
						field : 'progressStatusCode'
					});

			var _progressStatusCode = 0;

			if (_edprogressStatusCode) {
				var _statusMsg = $(_edprogressStatusCode.target).combobox(
						'getText');
				if (_statusMsg != '连载中' && _statusMsg != '已完结') {
					$.messager.alert('提示', '未知进度', 'warning');
					return;
				}
				_progressStatusCode = $(_edprogressStatusCode.target).combobox(
						'getValue');
			} else {
				_progressStatusCode = notEntityVideoEditRow.statusCode;
			}

			//验证标题
			var _edTitle = videoNotEntityListDatagrid.treegrid('getEditor', {
				id : notEntityVideoEditRow.id,
				field : 'title'
			});

			var _Title = '';
			if (_edTitle) {
				_Title = $(_edTitle.target).val();
			} else {
				_Title = notEntityVideoEditRow.title;
			}

			if (!_Title || _Title.trim() == '') {
				$.messager.alert('提示', '标题不能为空', 'warning');
				return;
			}

			//获取简介
			var _edSummary = videoNotEntityListDatagrid.treegrid('getEditor', {
				id : notEntityVideoEditRow.id,
				field : 'summary'
			});

			var _Summary = '';

			if (_edSummary) {
				_Summary = $(_edSummary.target).val();
			} else {
				_Summary = notEntityVideoEditRow.summary;
			}

			//获取详情
			var _edInfo = videoNotEntityListDatagrid.treegrid('getEditor', {
				id : notEntityVideoEditRow.id,
				field : 'info'
			});

			var _Info = '';

			if (_edInfo) {
				_Info = $(_edInfo.target).val();
			} else {
				_Info = notEntityVideoEditRow.info;
			}

			//获取排序
			var _edSort = videoNotEntityListDatagrid.treegrid('getEditor', {
				id : notEntityVideoEditRow.id,
				field : 'sort'
			});

			var _Sort = $(_edSort.target).val();

			var _Data = {
				id : notEntityVideoEditRow.id,
				title : _Title,
				sort : _Sort,
				summary : _Summary,
				info : _Info,
				status : _StatusCode,
				progressStatus : _progressStatusCode
			};

			$.ajax({
				data : _Data,
				async : false,
				type : 'POST',
				url : baseUrl + '/video/update',
				success : function(data) {
					if (data.responseCode == '000') {
						var _edStatusCode = videoNotEntityListDatagrid
								.treegrid('getEditor', {
									id : notEntityVideoEditRow.id,
									field : 'statusCode'
								});
						if (_edStatusCode) {
							notEntityVideoEditRow.statusMsg = $(
									_edStatusCode.target).combobox('getText');
						}

						var _edProgressStatusCode = videoNotEntityListDatagrid
								.treegrid('getEditor', {
									id : notEntityVideoEditRow.id,
									field : 'progressStatusCode'
								});
						if (_edProgressStatusCode) {
							notEntityVideoEditRow.progressStatusMsg = $(
									_edProgressStatusCode.target).combobox(
									'getText');
						}

						videoNotEntityListDatagrid.treegrid('endEdit',
								notEntityVideoEditRow.id);

						$.messager.alert('提示', '修改成功', 'info');
					} else {
						$.messager.alert('提示', data.responseMessage, 'error');
					}
				}
			});

		}
	}

	function videoNotEntityList_categoryListPage() {
		var videoSelect = videoNotEntityListDatagrid.treegrid('getSelected');
		if (videoSelect) {
			$.ajax({
				data : {
					itemId : videoSelect.id,
					itemType : 1
				},
				async : false,
				url : baseUrl + '/category/set.html',
				success : function(html) {
					$('body').append(html);
				}
			});
		}
	}

	function videoNotEntityList_tagListPage() {
		var videoSelect = videoNotEntityListDatagrid.treegrid('getSelected');
		if (videoSelect) {
			$.ajax({
				data : {
					itemId : videoSelect.id,
					itemType : 1
				},
				async : false,
				url : baseUrl + '/tag/set.html',
				success : function(html) {
					$('body').append(html);
				}
			});
		}
	}

	function videoNotEntityChildList_config() {
		var videoSelect = videoNotEntityListDatagrid.treegrid('getSelected');

		if (videoSelect
				&& videoNotEntityListDatagrid.treegrid('getLevel',
						videoSelect.id) == 1) {
			$.ajax({
				data : {
					itemId : videoSelect.id,
					itemType : 1
				},
				async : false,
				url : baseUrl + '/video/children-config.html?itemId='
						+ videoSelect.id,
				success : function(html) {
					$('body').append(html);
				}
			});
		}
	}
</script>


<script type="text/javascript">
	// 	$.extend($.fn.treegrid.methods, {
	// 		editCell : function(jq, param) {
	// 			return jq.each(function() {
	// 				var opts = $(this).treegrid('options');
	// 				var fields = $(this).treegrid('getColumnFields', true).concat(
	// 						$(this).treegrid('getColumnFields'));
	// 				for ( var i = 0; i < fields.length; i++) {
	// 					var col = $(this).treegrid('getColumnOption', fields[i]);
	// 					col.editor1 = col.editor;
	// 					if (fields[i] != param.field) {
	// 						col.editor = null;
	// 					}
	// 				}
	// 				$(this).treegrid('beginEdit', param.id);
	// 				for ( var i = 0; i < fields.length; i++) {
	// 					var col = $(this).treegrid('getColumnOption', fields[i]);
	// 					col.editor = col.editor1;
	// 				}
	// 			});
	// 		}
	// 	});
</script>
